#include "compi/compi.h"
#include "compi/p2p.h"
#include "compi/result.h"

#include <gtest/gtest.h>

using namespace compi;

class UnifiedErrorHandlingTest : public ::testing::Test
{
protected:
    // 基础测试不需要MPI环境
};

// 测试Result<T>类的基本功能
TEST_F(UnifiedErrorHandlingTest, ResultBasicFunctionality)
{
    // 测试成功的Result
    auto success_result = Result<int>::success(42);
    EXPECT_TRUE(success_result.is_success());
    EXPECT_FALSE(success_result.is_error());
    EXPECT_EQ(success_result.value(), 42);
    EXPECT_EQ(success_result.error_code(), ResultCode::Success);

    // 测试错误的Result
    auto error_result = Result<int>::error(ResultCode::MpiError, "Test error");
    EXPECT_FALSE(error_result.is_success());
    EXPECT_TRUE(error_result.is_error());
    EXPECT_EQ(error_result.error_code(), ResultCode::MpiError);
    EXPECT_EQ(error_result.error_message(), "Test error");
}

// 测试Result<T>的便利宏
TEST_F(UnifiedErrorHandlingTest, ResultMacros)
{
    // 测试COMPI_SUCCESS宏
    auto success_result = COMPI_SUCCESS(100);
    EXPECT_TRUE(success_result.is_success());
    EXPECT_EQ(success_result.value(), 100);

    // 测试COMPI_ERROR宏
    auto error_result = COMPI_ERROR(int, ResultCode::InvalidRequest, "Invalid request");
    EXPECT_TRUE(error_result.is_error());
    EXPECT_EQ(error_result.error_code(), ResultCode::InvalidRequest);
    EXPECT_EQ(error_result.error_message(), "Invalid request");
}

// 测试TransportRequest::result()方法
TEST_F(UnifiedErrorHandlingTest, TransportRequestResult)
{
    // 测试空的TransportRequest
    TransportRequest empty_req;
    auto result = empty_req.result();
    EXPECT_TRUE(result.is_error());
    EXPECT_EQ(result.error_code(), ResultCode::InvalidRequest);
    EXPECT_EQ(result.error_message(), "TransportRequest has no implementation");
}

// 注意：TypedChannel的send_result和recv_result方法的测试需要多进程MPI环境
// 这些方法的接口已经在头文件中定义，编译时会验证类型正确性

// 测试Result<T>的移动语义
TEST_F(UnifiedErrorHandlingTest, ResultMoveSemantics)
{
    auto create_success = []() -> Result<std::string>
    { return Result<std::string>::success("Hello, World!"); };

    auto create_error = []() -> Result<std::string>
    { return Result<std::string>::error(ResultCode::NetworkError, "Network failed"); };

    auto success_result = create_success();
    EXPECT_TRUE(success_result.is_success());
    EXPECT_EQ(success_result.value(), "Hello, World!");

    auto error_result = create_error();
    EXPECT_TRUE(error_result.is_error());
    EXPECT_EQ(error_result.error_code(), ResultCode::NetworkError);
}

// 测试Result<void>特化
TEST_F(UnifiedErrorHandlingTest, ResultVoidSpecialization)
{
    // 测试成功的void Result
    auto success_result = COMPI_SUCCESS_VOID();
    EXPECT_TRUE(success_result.is_success());
    EXPECT_EQ(success_result.error_code(), ResultCode::Success);

    // 测试错误的void Result
    auto error_result = Result<void>::error(ResultCode::Timeout, "Operation timed out");
    EXPECT_TRUE(error_result.is_error());
    EXPECT_EQ(error_result.error_code(), ResultCode::Timeout);
    EXPECT_EQ(error_result.error_message(), "Operation timed out");
}

// 测试Result<T>的链式操作（如果需要的话）
TEST_F(UnifiedErrorHandlingTest, ResultChaining)
{
    auto process_value = [](int x) -> Result<int>
    {
        if (x < 0)
            return COMPI_ERROR(int, ResultCode::InvalidRequest, "Negative value not allowed");
        return COMPI_SUCCESS(x * 2);
    };

    auto positive_result = process_value(5);
    EXPECT_TRUE(positive_result.is_success());
    EXPECT_EQ(positive_result.value(), 10);

    auto negative_result = process_value(-1);
    EXPECT_TRUE(negative_result.is_error());
    EXPECT_EQ(negative_result.error_code(), ResultCode::InvalidRequest);
}